package com.sam.core;

import java.util.Enumeration;
import java.util.ResourceBundle;

import org.apache.struts2.config.ParentPackage;

import com.sam.core.util.RequestUtils;

/**
 * js国际化辅助action
 * 将struts2中的国际化资源导出为js数组
 * @author Sam
 * @version 1.0
 */
@ParentPackage("default")
public class LocaleAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	@Override
	public String execute() throws Exception {
		StringBuffer sb = new StringBuffer();
		ResourceBundle rb = null;
		//获取国际化资源对象
		if (getSession().getAttribute(Constants.I18N_IN_SESSION) == null) {
			rb = getTexts(RequestUtils.getConfig("struts.custom.i18n.resources"));
			getSession().setAttribute(Constants.I18N_IN_SESSION, rb);
		} else {
			rb = (ResourceBundle) getSession().getAttribute(Constants.I18N_IN_SESSION);
		}
		//将国际化资源对象传化为json字符串
		Enumeration<String> keys = rb.getKeys();
		sb.append("EMAN_I18N={'ctx':'").append(getRequest().getContextPath()).append("',");
		for (; keys.hasMoreElements();) {
			String key = keys.nextElement();
			sb.append('\n').append("'").append(key).append("':'").append(rb.getString(key)).append(
					"',");
		}
		jsonString = sb.substring(0, sb.length() - 1) + "};";
		return JSON;
	}
}